package com.xmall.controller.admin;

import com.xmall.bean.bo.PermissionBo;
import com.xmall.bean.vo.BaseRespVo;
import com.xmall.bean.bo.CreateRoleBo;
import com.xmall.bean.bo.RoleListBo;
import com.xmall.bean.table.Role;
import com.xmall.bean.vo.ListDataVo;
import com.xmall.bean.vo.permission.PermissionsVo;
import com.xmall.bean.vo.RoleOptionListVo;
import com.xmall.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin/role")
public class RoleController {

    @Autowired
    RoleService roleService;

    @GetMapping("list")
    public BaseRespVo queryRoleList(RoleListBo roleListBo) {
        if (!roleListBo.isValid()) {
            return BaseRespVo.fail("参数无效，请重新输入");
        }
        ListDataVo<Role> roleListVo = roleService.queryRoleList(roleListBo);
        return BaseRespVo.ok(roleListVo);
    }

    @PostMapping("create")
    public BaseRespVo createRole(@RequestBody CreateRoleBo createRoleBo) {
        // 校验
        Role role = roleService.createRole(createRoleBo);
        return BaseRespVo.ok(role);
    }

    @PostMapping("delete")
    public BaseRespVo deleteRole(@RequestBody Role role) {
        roleService.deleteRole(role);
        return BaseRespVo.ok();
    }

    @PostMapping("update")
    public BaseRespVo updateRole(@RequestBody Role role) {
        roleService.updateRole(role);
        return BaseRespVo.ok();
    }

    @GetMapping("options")
    public BaseRespVo options() {
        List<RoleOptionListVo> roleOptionListVos = roleService.options();
        return BaseRespVo.ok(roleOptionListVos);
    }

    @GetMapping("permissions")
    public BaseRespVo permission(Integer roleId) {
        PermissionsVo permissionsVo = roleService.getAllPermissions(roleId);
        return BaseRespVo.ok(permissionsVo);
    }

    @PostMapping("permissions")
    public BaseRespVo authorization(@RequestBody PermissionBo permissionBo) {
        roleService.authorization(permissionBo);
        return BaseRespVo.ok();
    }

}
